#!/bin/bash
#检测weblogic server是否正常运行
#testWeblogic.sh

#捕捉异常退出
trap "error_exit" 1 2 3 24

###############################################################################
#配置文件
configFile="./test.cfg"

#weblogic信息
weblogicCfg="./testWeblogic.config"

#日志输出文件
logFile="./test.log"

#结果输出文件
resultFile="./result.log"

#关键日志的errorlog
weblogicLog="./weblogic.log"

#搜索weblogci关键日志的关键string
findLogString="<Error>"


#############################################################################
#本地环境weblogic.jar位置
#CLASSPATH

#要检查的weblogic数量
#WEBLOGIC_NUM

#调用webservice接口
#接口地址(不带IP和端口)
#CALL_SERVICE_ADDRESS

#报文
#CALL_MSG


#测试HTTP请求页面地址
#HTTP_URL


#测试HTTP请求返回页面的正确性
#HTTP_VALIDATION_STR

#测试调用接口返回是否正确
#CALL_MSG_VALIDATION_STR

##############################################################
>$logFile
>$resultFile
>lock.lock
>$weblogicLog
rm -f error.lock
#########################################Function########################################

#日志记录
note_log()
{
	local Str=$1
	echo -e "["`date +'%Y-%m-%d %H:%M:%S'`"]$Str" >> $logFile
}


#结果记录
note_result(){
	local Str=$1
	echo -e "$Str" >> $resultFile
}

#退出脚本
my_exit(){
	ti=$1
	if [ $ti -eq 1 ]
	then
		>error.lock
	fi
	rm -f *.temp
	rm -f lock.lock
	note_log "临时文件已删除，脚本已退出"
	exit $ti
}


#异常退出
error_exit(){
	rm -f *.temp
	rm -f lock.lock
	>error.lock
	note_log "脚本异常结束"
	exit 1
}

#检查配置文件
test_cfg(){
	note_log "开始检查配置文件${configFile},${weblogicCfg}"
	
	if [ ! -f ${configFile} ]
	then
		note_log "${configFile}配置文件不存在,请检查!"
		my_exit 1
	fi
	
	if [ ! -f ${weblogicCfg} ]
	then
		note_log "${weblogicCfg}配置文件不存在,请检查!"
		my_exit 1
	fi
	
	cat ${configFile}|sed '/^#/d'|sed '/^$/d' >configFile.temp
	cat ${weblogicCfg}|sed '/^#/d'|sed '/^$/d' >weblogicCfg.temp
}


#读取配置文件,获取全局配置信息
read_cfg(){
	note_log "开始获取全局配置信息"
	fgrep -A7 "[global_cfg]" configFile.temp |sed '1d' > aa.temp
	
	if [ ! -s aa.temp ]
	then
		note_log "没有获取到[global_cfg]全局配置信息,检查配置文件${configFile}"
		my_exit 1		
	fi
	
	while read LINE
	do
		eval echo \${$LINE} >/dev/null 2>&1
	done<aa.temp
		
	note_log "读取全局配置成功"
}

#远程连接主机的工具
testExpect(){
		loginStr=$1
		password=$2
		send_cmd=$3
		expect -c "
		spawn ssh ${loginStr}@${IP};
		expect \"*yes/no*\"  {send \"yes\r\";exp_continue}
		expect \"*password:*\"  {send \"$password\r\"}
		expect \"*from*\"  {send \"$send_cmd\r\"}
		set timeout 2
		send \"exit\r\"
		expect eof
		"
}



#调用webservice
call_service(){
	responseTime=`curl -o /dev/null -s -w %{time_total} -d "${CALL_MSG}" "http://${URL}/${CALL_SERVICE_ADDRESS}"`
	if [ "$?" -eq "0" ]
	then
		note_log "调用接口成功 响应时间 [$responseTime]"
		note_result "${LINE1}.callService.status=OK"
		note_result "${LINE1}.callService.responseTime=$responseTime"
	else
		note_log "调用接口失败。"
		note_result "${LINE1}.callService.status=FAIL"
	fi
}


#检查http返回的页面
call_http(){
	if [ ! "$HTTP_URL" == "" ]
	then
	curl -s ${HTTP_URL}|fgrep -q ${HTTP_VALIDATION_STR}
	
	if [ $? -eq 0 ]
	then
		note_log "请求HTTP成功,返回正确"
		note_result "${LINE1}.callHttp.status=OK"
	else
		note_log "请求HTTP不成功或者返回了错误的内容"
		note_result "${LINE1}.callHttp.status=FAIL"
	fi
	fi
}


#检查weblogic的详细信息
test_weblogic(){

		note_log "开始检查$URL $DOMAIN_NAME $SERVER_NAME ..."
		
		
		
		IP=`echo $URL|awk -F":" '{print $1}'`
		PORT=`echo $URL|awk -F":" '{print $2}'`
		
		#远程登陆webllogic主机
		cmd_1="ps auxwh|fgrep -v fgrep|fgrep java"
		
		note_log "远程登陆主机$IP...执行命令[${cmd_1}]"
		
		testExpect $LOGIN_STR $LOGIN_PASSWORD "${cmd_1}" >expectlog.temp
		
		rt=`fgrep "Connection" expectlog.temp` 2>/dev/null
		tt=`fgrep "--help" expectlog.temp` 2>/dev/null
		if [[ -n "$rt" && -z "$tt" ]]
		then
			note_log "远程登录主机$IP失败..."
			continue
		fi
		
		note_log "登陆主机$IP成功,执行命令[${cmd_1}]成功"
		
		#获取weblogic的java进程信息和CPU消耗
		cpu_num_1=`cat expectlog.temp|fgrep java|fgrep ${PORT}|awk '{print $3}'`
		#获取PID
		java_pid=`cat expectlog.temp|fgrep java|fgrep ${PORT}|awk '{print $2}'`
		if [ -z $cpu_num_1 ]
		then
			note_log "${IP}主机上面没有发现指定的weblogic的java进程信息,请检查进程是否已挂掉"
			note_result "${LINE1}.java.pid=0"
			continue
		fi
		
		note_log "当前进程PID${java_pid}消耗主机CPU的百分比为${cpu_num_1}%"	
		
		note_result	"${LINE1}.java.pid=${java_pid}\n${LINE1}.java.cpu=${cpu_num_1}"

		#获取weblogic关键日志
				
		#远程登陆webllogic主机
		cmd_2="fgrep -A200 ${findLogString} ${LOG_PATH}"
		note_log "远程登陆主机$IP...执行命令[${cmd_2}]"		
		testExpect $LOGIN_STR $LOGIN_PASSWORD "${cmd_2}" >expectlog.temp
		
		rt=`fgrep "Connection" expectlog.temp` 2>/dev/null
		tt=`fgrep "--help" expectlog.temp` 2>/dev/null
		if [[ -n "$rt" && -z "$tt" ]]
		then
			note_log "远程登录主机$IP失败..."
			continue
		fi
		
		note_log "登陆主机$IP成功,执行命令[${cmd_2}]成功"
		
		echo "[$URL $DOMAIN_NAME $SERVER_NAME $LOG_PATH]">>$weblogicLog
		
		fgrep -A200 ${findLogString} expectlog.temp >> $weblogicLog
		
		if [ $? -ne 0 ]
		then
			echo "没有搜索到指定的weblogic日志">>$weblogicLog	
		fi
		
		note_log "已保存搜索到的weblogic日志,日志路径$weblogicLog"
		
		note_result "${LINE1}.errorLog=${weblogicLog}"
		
		#验证是否能够执行weblogic.Admin
		
		> weblogicStatus.temp
		java weblogic.Admin -url $URL -username $USERNAME -password $PASSWORD get -pretty -mbean "$DOMAIN_NAME:Location=$SERVER_NAME,Name=$SERVER_NAME,Type=ServerRuntime" >/dev/null 2>&1
		
		if [ $? -ne 0 ]
		then
			note_log "执行命令weblogic.Admin命令失败,请检查环境变量配置"
			note_result "${LINE1}.AdminPermission=FAIL"
			my_exit 1
		fi
		
		note_result "${LINE1}.AdminPermission=OK"
		
		java weblogic.Admin -url $URL -username $USERNAME -password $PASSWORD get -pretty -mbean "$DOMAIN_NAME:Location=$SERVER_NAME,Name=$SERVER_NAME,Type=ServerRuntime" >weblogicStatus.temp 
		
		#检查运行状态
		fgrep -q "State: RUNNING" weblogicStatus.temp
		
		if [ $? == 0 ]; then
    	note_log "检查运行状态Run Status [OK]"
    	note_result "${LINE1}.runStatus=OK"
		else
    	note_log "检查运行状态Run Status [NOT OK]"
    	note_result "${LINE1}.runStatus=NOT OK"
		fi
	
		#检查健康状态	
		fgrep -q "State:HEALTH_OK" weblogicStatus.temp
		if [ $? == 0 ]; then
    	note_log "检查健康状态Health Status [OK]"
    	note_result "${LINE1}.healthStatus=OK"
		else
    	note_log "检查健康状态Health Status [NOT OK]"
    	note_result "${LINE1}.healthStatus=NOT OK"
		fi
		
		#检查打开的套接字数
		SOCKET_NOW=`cat weblogicStatus.temp | fgrep "OpenSocketsCurrentCount"|awk -F":" '{print $2}'`
		if [ "x$SOCKET_NOW" == "x" ]
		then
    	note_log "检查当前套接字数Open Sockets Number [FAIL TO GET]"
    	note_result "${LINE1}.openSocketsNumber=FAIL"
		else
			note_log "检查当前套接字数Open Sockets Number [OK] 当前打开数$SOCKET_NOW"
			note_result "${LINE1}.openSocketsNumber=$SOCKET_NOW"
		fi	
		
		#检查JVM使用情况
		note_log "开始检查当前weblogic的JVM使用情况"
		java weblogic.Admin -url $URL -username $USERNAME -password $PASSWORD get -pretty -type JVMRuntime|head -20 >weblogicStatus.temp
		
		#当前总大小
		jvm_curr_size=`fgrep "HeapSizeCurrent" weblogicStatus.temp|awk -F":" '{print $2}'`
		#最大值
		jvm_max_size=`fgrep "HeapSizeMax" weblogicStatus.temp|awk -F":" '{print $2}'`
		#当前空闲
		jvm_free_size=`fgrep "HeapFreeCurrent" weblogicStatus.temp|awk -F":" '{print $2}'`
		#空闲百分比
		jvm_free_percent=`fgrep "HeapFreePercent" weblogicStatus.temp|awk -F":" '{print $2}'`
		
		if [[ -n ${jvm_curr_size} && -n ${jvm_max_size} && -n ${jvm_free_size} && -n ${jvm_free_percent} ]]
		then
			note_log "检查JVM使用情况完毕:"
			note_log "max_size=${jvm_max_size},current_size=${jvm_curr_size},free_size=${jvm_free_size},free_percent=${jvm_free_percent}"
			note_result "${LINE1}.jvm.maxSize=${jvm_max_size}\n${LINE1}.jvm.currentSize=${jvm_curr_size}\n${LINE1}.jvm.freeSize=${jvm_free_size}\n${LINE1}.jvm.freePercent=${jvm_free_percent}"
		else
			note_log "执行weblogic.Admin命令失败,请检查"
			note_result "${LINE1}.jvm=FAIL"
		fi
		
		#检查队列情况
		note_log "开始检查当前weblogic的队列使用情况"
	  java weblogic.Admin -url $URL -username $USERNAME -password $PASSWORD get -pretty -type ExecuteQueueRuntime >weblogicStatus.temp
	  
    #当前空闲线程数   
    queue_free_count=`fgrep "ExecuteThreadCurrentIdleCount" weblogicStatus.temp|awk -F":" '{print $2}'`
    #等待线程数
    queue_pending_count=`fgrep "PendingRequestCurrentCount" weblogicStatus.temp|awk -F":" '{print $2}'`
		
		if [[ -n ${queue_free_count} && -n ${queue_pending_count} ]]
		then
			note_log "检查队列情况完毕:"
			note_log "free_count=${queue_free_count},pending_count=${queue_pending_count}"
			note_result "${LINE1}.queue.freeCount=${queue_free_count}\n${LINE1}.queue.pendingCount=${queue_pending_count}"
		else
			note_log "执行weblogic.Admin命令失败,请检查"
			note_result "${LINE1}.queue=FAIL"
		fi
		
		#调用接口
		call_service	
		
		#请求http
		call_http
		
}


#检查weblogic
test_step(){
	
	note_log "开始检查weblogic..."
	
	#获取检查列表信息
	note_log "获取需要检查的weblogic列表"
	
	fgrep -A${WEBLOGIC_NUM} "[test_list]" configFile.temp |sed '1d' > aa.temp
	
	while read LINE1
	do
		unset URL USERNAME PASSWORD DOMAIN_NAME SERVER_NAME LOG_PATH LOGIN_STR LOGIN_PASSWORD
		note_result "[$LINE1]"	
			
		fgrep -A8 "[${LINE1}]" weblogicCfg.temp | sed '1d' >bb.temp
		
		if [ ! -s bb.temp ]
		then
			note_log "没有获取到[${LINE1}]标签weblogic的详细信息,请检查配置文件${weblogicCfg},跳过${LINE1}的测试"
			continue		
		fi
	
		while read LINE2
		do
			eval echo \${$LINE2} >/dev/null 2>&1
		done<bb.temp
		
		note_result "${LINE1}.url=${URL}"
				
		note_log "读取[${LINE1}]标签weblogic的详细信息成功："
		note_log "URL=${URL},USERNAME=${USERNAME},PASSWORD=${PASSWORD},DOMAIN_NAME=${DOMAIN_NAME},SERVER_NAME=${SERVER_NAME},LOG_PATH=${LOG_PATH},LOGIN_STR=${LOGIN_STR},LOGIN_PASSWORD=${LOGIN_PASSWORD}"
		
		#开始检测
		test_weblogic
		
	done<aa.temp	
		
	
}

###############################################################FUNCTION########################################################

#脚本开始
		
test_cfg #检查配置文件
read_cfg #读取全局配置信息
test_step #测试方法
my_exit 0  #正常退出

